package com.cg.design.graphicdesignpattern.interpreter.language;

/**
 * 使解释器更好用的类，Facade模式中的Facade角色
 *
 * @program: LeetCode->InterpreterFacade
 * @author: cg
 * @create: 2022-04-06 09:31
 **/
public class InterpreterFacade implements Executor {

    private final ExecutorFactory factory;
    private Context context;
    private BaseNode programNode;

    public InterpreterFacade(ExecutorFactory factory) {
        this.factory = factory;
    }

    public boolean parse(String text) {
        boolean ok = true;
        context = new Context(text);
        context.setExecutorFactory(factory);
        programNode = new ProgramNode();
        try {
            programNode.parse(context);
            System.out.println(programNode.toString());
        } catch (ParseException e) {
            e.printStackTrace();
            ok = false;
        }
        return ok;
    }

    @Override
    public void execute() throws ExecuteException {
        try {
            programNode.execute();
        }catch (ExecuteException e) {
            e.printStackTrace();
        }
    }
}
